package com.zhu.common.intercaptor;

import com.zhu.user.model.User;
import com.zhu.user.util.SysUtil;
import com.zhu.util.EmptyUtil;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by DELL on 2016/1/5.
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if((request.getContextPath() + "/login").equals(request.getRequestURI())){
            if(EmptyUtil.notNUllAndEmpty(SysUtil.getCurrentUsername())){
                response.sendRedirect(request.getContextPath() + "/");
            }

            return true;
        }else {

            if (EmptyUtil.isNullOrEmpty(SysUtil.getCurrentUsername())) {
                response.sendRedirect(request.getContextPath() + "/login");

                return false;
            } else {
                User user = SysUtil.getCurrentUser();
                if (user == null) {
                    response.sendRedirect(request.getContextPath() + "/login");

                    return false;
                }
            }
        }

        return true;
    }
}
